"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : describe table
Case Name   : desc/describe查询执行计划，format参数验证，format=format_name格式，
            取值text/xml/yaml带引号需全部小写
Create At   : 2023-01-12
@zou_jialiang0501162244
Description :
    1.兼容B库建表并插入数据
    2.desc/describe查询执行计划，format取值text/xml/yaml带引号，小写
    3.desc/describe查询执行计划，format取值text/xml/yaml带引号，大小写
    4.清理环境
Expect      :
    1.成功
    2.输出对应格式计划
    3.合理报错
    4.成功
History     :
    modified by @zou_jialiang050,2023.04.13，指定SQL语句执行库为兼容B库
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Describe(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.tb_name = 't_describe_0047'
        self.db_name = 'db_describe_0047'
        self.log.info('----创建兼容B库----')
        create_db = f"drop database if exists {self.db_name};" \
                    f"create database {self.db_name} dbcompatibility='B';"
        res_createdb = self.pri_sh.execut_db_sql(create_db)
        self.log.info(res_createdb)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res_createdb,
                      f"创建兼容B库失败")

    def test_describe_table(self):
        text1 = '-----step1:建表并插入数据;expect:成功-----'
        self.log.info(text1)
        create_tb = f'''drop table if exists {self.tb_name};
        create table {self.tb_name}(
        c_int int primary key, c_varchar varchar(20), c_text text);
        insert into {self.tb_name} values
        (1, 'azAZ09—字符', lpad('abc', '50', 'a@123汉字&^%djgk')), 
        (2, 'azAZ09—字符', lpad('abc', '50', 'a@123汉字&^%djgk'));'''
        res_createtb = self.pri_sh.execut_db_sql(
            sql=create_tb, dbname=f"{self.db_name}")
        self.log.info(res_createtb)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res_createtb,
                      f'建表失败:{text1}')
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, res_createtb,
                      f'插入数据失败:{text1}')

        text2 = '-----step2:desc/describe查询执行计划，format取值' \
                'text/xml/yaml带引号，小写;expect:输出对应格式计划-----'
        self.log.info(text2)
        desc_sql = f'''set explain_perf_mode = normal;
        desc format = 'text' select c_int, c_varchar from {self.tb_name};
        desc format = 'xml' select c_int, c_varchar from {self.tb_name};
        describe format = 'yaml' select * from {self.tb_name};'''
        res1 = self.pri_sh.execut_db_sql(
            sql=desc_sql, dbname=f"{self.db_name}")
        self.log.info(res1)
        self.assertIn('Seq Scan on', res1, f'执行失败:{text2}')
        self.assertIn('<Node-Type>Seq Scan</Node-Type>', res1,
                      f'执行失败:{text2}')
        self.assertIn('Node Type: "Seq Scan"', res1, f'执行失败:{text2}')

        text3 = '-----step3:desc/describe查询执行计划，format取值' \
                'text/xml/yaml带引号，大小写混合;expect:合理报错-----'
        self.log.info(text3)
        desc_err = f'''set explain_perf_mode = normal;
        describe format = 'TEXT' select * from {self.tb_name};
        describe format = 'TExt' select * from {self.tb_name};
        describe format = 'XML' select * from {self.tb_name};
        describe format = 'xmL' select * from {self.tb_name};
        desc format = 'Yaml' select * from {self.tb_name};
        desc format = 'YAML' select * from {self.tb_name}'''
        res_err = self.pri_sh.execut_db_sql(
            sql=desc_err, dbname=f"{self.db_name}")
        self.log.info(res_err)
        self.assertEqual(res_err.count('ERROR'), 6, f'执行失败:{text3}')

    def tearDown(self):
        text4 = '-----step4:清理环境;expect:成功-----'
        self.log.info(text4)
        drop_db = self.pri_sh.execut_db_sql(f"drop database {self.db_name};")
        self.log.info(drop_db)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_db,
                      f'执行失败:{text4}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
